package com.itheima.threadtest7;

import com.itheima.threadtest6_2.MyThread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

public class MyCallable implements Callable<Integer> {

    ArrayList<Integer> list;
    public MyCallable(ArrayList<Integer> list) {
        this.list = list ;
    }

    @Override
    public Integer call() throws Exception {
        ArrayList<Integer> boxList = new ArrayList<>() ;    // 每个线程会创建自己的集合
        while(true) {
            synchronized (MyThread.class) {
                if (list.isEmpty()) {
                    // 抽奖结束
                    System.out.println(Thread.currentThread().getName() + "：" + boxList);
                    break ;
                } else {
                    Collections.shuffle(list);
                    int prize = list.remove(0) ;
                    boxList.add(prize) ;
                }
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
        //把集合的最大值返回回去
        if (boxList.size() == 0) {
            return null ;
        } else {
            return Collections.max(boxList) ;
        }
    }
}
